package openjdk.jdk8.demo.chap3;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class ExecuteAround {

	public static void main(String ...args) throws IOException{

        // method we want to refactor to make more flexible
        String result = processFileLimited();
        System.out.println(result);

        System.out.println("---");

		String oneLine = processFile((BufferedReader b) -> b.readLine());
		System.out.println(oneLine);

		String twoLines = processFile((BufferedReader b) -> b.readLine() + b.readLine());
		System.out.println(twoLines);

	}

    public static String processFileLimited() throws IOException {

//		try {
//			URI uri = ExecuteAround.class.getResource("/jdk8/chap3/data.txt").toURI();
//			List<String> lst = Files.readAllLines(
//					Paths.get(uri), Charset.defaultCharset());
//			Object x = lst;
//		} catch (URISyntaxException e) {
//			e.printStackTrace();
//		}

		try (BufferedReader br =
                     new BufferedReader(new FileReader("jdk8/chap3/data.txt"))) {
            return br.readLine();
        }
    }


	public static String processFile(BufferedReaderProcessor p) throws IOException {
		try(BufferedReader br = new BufferedReader(new FileReader("jdk8/chap3/data.txt"))){
			return p.process(br);
		}

	}

	public interface BufferedReaderProcessor{
		public String process(BufferedReader b) throws IOException;

	}
}
